$(function () {
    $("#jqGrid").jqGrid({
        url: '../employeeperformancemanagement/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '员工姓名', name: 'empName', index: 'emp_name', width: 80 },
            { label: '员工id', name: 'empId', index: 'emp_id', width: 80 ,hidden:true},
            { label: '公司', name: 'company', index: 'company', width: 80 },
            { label: '公司id', name: 'companyId', index: 'company_id', width: 80,hidden:true },
            { label: '部门', name: 'dept', index: 'dept', width: 80 },
            { label: '岗位', name: 'post', index: 'post', width: 80 ,formatter:getDictInfo},
            { label: '岗位id', name: 'postId', index: 'post_id', width: 80,hidden:true },
            { label: '年', name: 'year', index: 'year', width: 80 },
            { label: '月', name: 'mouth', index: 'mouth', width: 80 },
            { label: '基本工资', name: 'baseSalary', index: 'base_salary', width: 80 },
            { label: '绩效方式', name: 'achievementsType', index: 'Achievements_type', width: 80 ,
                formatter:function(v){
                    if(v=="0"){
                        return "绩效比例";
                    }else{
                        return "绩效金额"
                    }
                } },
            { label: '绩效效果', name: 'achievementsResult', index: 'Achievements_result', width: 80 },
            { label: '其他补助', name: 'otherSubsidy', index: 'other_subsidy', width: 80 },
            { label: '其他', name: 'other', index: 'other', width: 80,hidden:true },
            { label: '录入人', name: 'cusername', index: 'cusername', width: 80 },
            { label: '录入日期', name: 'cdate', index: 'cdate',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  },
            { label: '是否锁定', name: 'islocked', index: 'isLocked', width: 80 ,
                formatter:function(v){
                    if(v=="0"){
                        return "是";
                    }else{
                        return "否"
                    }
                } }
        ],
		viewrecords: true,
        height: 215,
        rowNum: 5,
		rowList : [5,10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            year: null
        },
		showList: true,
        showTable:false,
        show1:false,
        show2:false,
		title: null,
		employeePerformanceManagement: {},
        evaluationInformationList: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
        addCertificateItem: function(){
            this.evaluationInformationList.push({
                achievementsContent:'',
                weightScore:'',
                selfRating:'',
                selfInstruction:'',
                superiorScoring:'',
                superiorInstruction:''
            });
        },
        delCertificateItem: function(index){
            this.evaluationInformationList.splice(index,1);
        },
		add: function(){
			vm.showList = false;
            vm.showTable=false;
            vm.show1=false;
            vm.show2=false;
			vm.title = "新增";
			vm.employeePerformanceManagement = {
                achievementsType:0,
                islocked:1
			};
            vm.evaluationInformationList = [{
               /* empName:'',
                empId:'',
                department:'',
                departmentId:'',
                post:'',
                postId:'',
                year:'',
                mouth:'',*/
                achievementsContent:'',
                weightScore:'',
                selfRating:'',
                selfInstruction:'',
                superiorScoring:'',
                superiorInstruction:''
                // achievementsId:''
            }];
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.islocked=='是')
            {
                alert("数据已经锁定，不能修改");
                return ;
            }

			vm.showList = false;
            vm.showTable=false;
            vm.show1=false;
            vm.show2=false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    var self="Y";//如果是领导评分，则要记下评分人
           if(vm.show2){
               self="N";
           }
            var evaluationInformationList= vm.evaluationInformationList;
            for(var  e in evaluationInformationList){
                var po=evaluationInformationList[e];
                if(po.weightScore==''||po.weightScore==null){
                    alert("权重评分不能为空");
                    return ;
                }
                if(po.achievementsContent==''||po.achievementsContent==null){
                    alert("绩效内容不能为空");
                    return ;
                }
            }

           if(vm.show1){//是评分阶段
               if(vm.show2){//领导评分
                   if(vm.employeePerformanceManagement.achievementsResult==''||vm.employeePerformanceManagement.achievementsResult==null){
                       alert("绩效效果不能为空");
                       return ;
                   }
               }else{

                   for(var  e in evaluationInformationList){
                       var po=evaluationInformationList[e];
                       if(po.selfRating==''||po.selfRating==null){
                           alert("自我评分不能为空");
                           return ;
                       }
                       if(po.selfInstruction==''||po.selfInstruction==null){
                           alert("自我说明不能为空");
                           return ;
                       }

                   }

               }
           }
               var url = vm.employeePerformanceManagement.id == null ? "../employeeperformancemanagement/saveAndUpdateKpxx" : "../employeeperformancemanagement/update";
			var params={"employeePerformanceManagement":vm.employeePerformanceManagement,
                "evaluationInformationList":vm.evaluationInformationList,
            "self":self};
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(params),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			for (var i=0;i<ids.length;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                    if(rowData.islocked=='是')
                    {
                        alert("数据已经锁定，不能修改");
                        return ;
                    }
            }
            var rowDatas=$("#jqGrid").jqGrid("getRowData",ids);
			for (var rowData in rowDatas){
            if(rowData.islocked=='是')
            {
                alert("数据已经锁定，不能修改");
                return ;
             }
            }
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../employeeperformancemanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},

        lock: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要锁定选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../employeeperformancemanagement/lock",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
		getInfo: function(id){
			$.get("../employeeperformancemanagement/info/"+id, function(r){
                vm.employeePerformanceManagement = r.employeePerformanceManagement;
                vm.evaluationInformationList= r.evaluationInformationList;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.show1=false;
            vm.show2=false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'year': vm.q.year,
                    'mouth': vm.q.mouth,'empName': vm.q.empName,'company':vm.q.company,
                    'dept': vm.q.dept},
                page:page
            }).trigger("reloadGrid");
		},
        selectKPjl: function(){
        var id = getSelectedRow();
        if(id==null){
            return ;
        }
        vm.showTable = true;

        $("#jqGridTable").jqGrid({
            url: '../employeeperformancemanagement/selectKPjl',
            datatype: "json",
            postData: {"id":id},
            colModel: [
                { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
               { label: '员工姓名', name: 'empName', index: 'emp_name', width: 80 },
                /* { label: '员工id', name: 'empId', index: 'emp_id', width: 80,hidden:true },
                { label: '部门', name: 'department', index: 'department', width: 80 },
                { label: '岗位', name: 'post', index: 'post', width: 80 },
                { label: '岗位id', name: 'postId', index: 'post_id', width: 80,hidden:true },
                { label: '年', name: 'year', index: 'year', width: 80 },
                { label: '月', name: 'mouth', index: 'mouth', width: 80 },*/
                { label: '绩效内容', name: 'achievementsContent', index: 'Achievements_content', width: 80 },
                { label: '权重分值', name: 'weightScore', index: 'Weight_score', width: 80 },
                { label: '自我评分', name: 'selfRating', index: 'Self_rating', width: 80 },
                { label: '自评说明', name: 'selfInstruction', index: 'Self_instruction', width: 120 },
                { label: '上级评分', name: 'superiorScoring', index: 'Superior_scoring', width: 80 }	,
                { label: '上级评分说明', name: 'superiorInstruction', index: 'Superior_instruction', width: 120 }	,
                { label: '录入人', name: 'cusername', index: 'cusername', width: 80 },
                { label: '录入日期', name: 'cdate', index: 'cdate',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  },
                { label: '审核人', name: 'approver', index: 'approver', width: 80 },
                { label: '审核时间', name: 'approveDate', index: 'approve_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'} },


            ],
            viewrecords: true,
            height: 385,
            rowNum: 100,
            //rowList : [10,30,50],
            rownumbers: true,
            //rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            shrinkToFit:false,
            autoScroll: false,
            pager: "#jqGridPagerTable",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
			/*prmNames : {
			 page:"page",
			 rows:"limit",
			 order: "order"
			 },*/
            gridComplete:function(){
                //隐藏grid底部滚动条
                //$("#jqGridTable").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        });
            $("#jqGridTable").jqGrid("clearGridData");
            // $("#jqGridTable").jqGrid('getGridParam','page');
            $("#jqGridTable").jqGrid('setGridParam',{
                url: '../employeeperformancemanagement/selectKPjl',
                datatype: "json",
                postData: {"id":id}
            }).trigger("reloadGrid");

    },
        selfRating: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.islocked=='是')
            {
                alert("数据已经锁定，不能修改");
                return ;
            }

            vm.showList = false;
            vm.showTable=false;
            vm.show1=true;
            vm.show2=false;
            vm.title = "自评";

            vm.getInfo(id)
        },

        superiorScoring: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.islocked=='是')
            {
                alert("数据已经锁定，不能修改");
                return ;
            }
            vm.show1=true;
            vm.show2=true;
            vm.showList = false;
            vm.showTable=false;
            vm.title = "总评";

            vm.getInfo(id)
        },

	}
});